package com.Dao.impl;

import com.Dao.PersonDao;
import com.entity.Person;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;

@Repository
public class PersonDaoImpl implements PersonDao {
    @Resource
    private JdbcTemplate jdbcTemplate;
    /*
    @Resource
    private NamedParameterJdbcTemplate namedParameterJdbcTemplate;
*/
    @Override
    public int addPerson(Person person) {
        String sql=
                "INSERT INTO Person(user_id,user_name,user_password,user_sex,user_age," +
                        "user_pro,user_hobby,following_list,fan_list,following_num,fan_num,user_lock) " +
                        "VALUES(?,?,?,?,?,?,?,?,?,?,?,?)";
        int update=jdbcTemplate.update(sql, person.getUser_id(),person.getUser_name(),
                person.getUser_password(),person.getUser_sex(),person.getUser_age(),
                person.getUser_pro(), person.getUser_hobby(), person.getFollowing_list(),
                person.getFan_list(), person.getFollowing_num(), person.getFan_num(),
                person.getUser_lock());
        return update;
    }

    @Override
    public int updatePerson(Person person) {
        String sql=
                "UPDATE Person SET user_name=?,user_password=?,user_sex=?,user_age=?," +
                        "user_pro=?,user_hobby=?,following_list=?,fan_list=?,following_num=?," +
                        "fan_num=?,user_lock=? " +
                        "WHERE user_id=?";
        int update=jdbcTemplate.update(sql,person.getUser_name(),
                person.getUser_password(),person.getUser_sex(),person.getUser_age(),
                person.getUser_pro(), person.getUser_hobby(), person.getFollowing_list(),
                person.getFan_list(), person.getFollowing_num(), person.getFan_num(),
                person.getUser_lock(),person.getUser_id());
        return update;
    }
    public Person getPersonById(int user_id){
        String sql="SELECT * FROM Person WHERE user_id=?";
        return jdbcTemplate.queryForObject(sql,new BeanPropertyRowMapper<>(Person.class),user_id);
    }
    public Person getPersonByName(String user_name){
        String sql="SELECT * FROM Person WHERE user_name=?";
        return jdbcTemplate.queryForObject(sql,new BeanPropertyRowMapper<>(Person.class),user_name);
    }
    public int countPersonNum(){
        String sql = "SELECT COUNT(*) FROM Person";
        return jdbcTemplate.queryForObject(sql, Integer.class);
    }

    @Override
    public int deletePerson(Person person) {
        String sql="DELETE FROM Person WHERE user_id=?";
        return jdbcTemplate.update(sql,person.getUser_id());
    }
    /*
    @Override
    public int updateUser(User user) {
        return 0;
    }

    @Override
    public int deleteUser(User user) {
        return 0;
    }

    @Override
    public User getUser(User user) {
        return null;
    }

 */
}
